package com.sie.controller;

import com.sie.pojo.UserManager;
import com.sie.service.UserManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class UserManagerController {

    @Autowired
    private UserManagerService userManagerService;

    /**
     * 列表查询所有
     * @return
     */
    @ResponseBody
    @RequestMapping("/managerlist")
    public List<UserManager> findAll() {
        List<UserManager> list = userManagerService.findAll();
        return list;
    }

    /**
     * 添加用户
     * @param userManager
     * @return
     */
    @ResponseBody
    @RequestMapping("/addUserManagerList")
    public int addUserManager(UserManager userManager) {
        System.out.println(userManager);
        return userManagerService.addUserManager(userManager);
    }

    /**
     * 多选框中根据id删除
     * @param ids  获取前端多选框中所有的id
     * @return
     */
    @ResponseBody
    @RequestMapping("/removeManager")
    public int removeManager(String ids) {
        System.out.println(ids);
        return userManagerService.removeManager(ids);

    }

    /***
     * 根据表单中的数据值进行查找
     * @param builtime  时间日期需要单独处理
     * @param ruleForm  UserManager对象
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/selectByForm")
    public List<UserManager> selectByForm(String builtime, UserManager ruleForm) throws Exception {
        Date date1;
        Date date2;
//        System.out.println(builtime);
        if (builtime.equals("0")) {
            date1 = date2 = null;
        } else {
            String[] split = builtime.split(",");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date1 = sdf.parse(split[0]);
            date2 = sdf.parse(split[1]);
        }
        List<UserManager> userManager = userManagerService.selectByForm(ruleForm.getUsername(),
                ruleForm.getName(),
                ruleForm.getStatus(),
                date1, date2);
//        System.out.println(userManager);
        return userManager;
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteById")
    public int deleteById(Integer id) {
        System.out.println(id);
        return userManagerService.deleteById(id);

    }

    /**
     * 跟新用户状态
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateStatusById")
    public int updateStatusById(Integer id) {
        System.out.println(id);
        return userManagerService.updateStatusById(id);

    }

    /**
     * 跟新用户列表
     * @param userManager
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateUserManager")
    public int updateUserManager(UserManager userManager) {
        System.out.println(userManager);
        System.out.println(userManagerService.updateUserManager(userManager));
        return userManagerService.updateUserManager(userManager);
    }

}
